const moment = require("moment");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
let date = moment().format("YYYYMMDD-HHmm");
const outPutDir = process.env.VUE_APP_OUTDIR + "-" + date;
const basePath = process.env.NODE_ENV === "production" ? "./" : "/";
const path = require("path");
const CompressionWebpackPlugin = require('compression-webpack-plugin')
// 定义压缩文件类型
const productionGzipExtensions = ['js', 'css']
const webpack = require('webpack')

module.exports = {
  outputDir: outPutDir,
  publicPath: basePath,
  assetsDir: "./static",
  configureWebpack: {
    optimization: {
      minimizer: [
        new UglifyJsPlugin({
          uglifyOptions: {
            warnings: false,
            compress: {
              drop_console: true, // console
              drop_debugger: false,
              pure_funcs: ["console.log"] // 移除console
            }
          }
        })
      ]
    },
    plugins: [
      new CompressionWebpackPlugin({
        filename: '[path].gz[query]',
        algorithm: 'gzip',
        test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
        threshold: 10240,
        minRatio: 0.8
      })
    ],
    externals: {
      vue: "Vue",
      vant: "vant",
      // "vue-router": "VueRouter",
      // vuex: "Vuex",
      // 'babel-polyfill': 'babel-polyfill',
      // "vue-lazyload": "VueLazyload",
      // axios: "axios"
    }
  },
  chainWebpack: config => {
    config.plugins.delete('prefetch');
    // config
    //   .plugin('webpack-bundle-analyzer')
    //   .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin);
    config.plugin('webpack-ignore').use(webpack.IgnorePlugin, [
      {
        resourceRegExp: /^\.\/locale$/,
        contextRegExp: /moment$/
      }
    ])
  },
  css: {
    loaderOptions: {
      stylus: {
        "resolve url": true,
        import: []
      },
      sass: {
        data: `$baseUrl: '${basePath}'; $staticPath: '${process.env.VUE_APP_STATIC}'; $logoPath: '${process.env.VUE_APP_LOGO}';`
      }
    }
  },
  devServer: {
    // 设置代理
    proxy: {
      "/member/*": {
        target: "https://98pay-members-dev.163163.app:9897/member",
        ws: false, // 是否启用websockets
        changOrigin: true, // 开启代理：在本地会创建一个虚拟服务端，然后发送请求的数据，并同时接收请求的数据，这样服务端和服务端进行数据的交互就不会有跨域问题
        pathRewrite: {
          "^/member": ""
        }
      },
      "/": {
        target: "https://98pay-members-dev.163163.app:9897",
        ws: false, // 是否启用websockets
        changOrigin: true, // 开启代理：在本地会创建一个虚拟服务端，然后发送请求的数据，并同时接收请求的数据，这样服务端和服务端进行数据的交互就不会有跨域问题
        pathRewrite: {
          "^/": ""
        }
      }
    }
  }
};
